//
//  CZCommentController.swift
//  GZWeibo666
//
//  Created by 123456 on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

class CZCommentController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 准备UI
        prepareUI()
        
        view.backgroundColor = UIColor.whiteColor()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "back")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendMessage")
        let tV = CZCommentTitleView()
        tV.frame = CGRectMake(0, 0, 50, 50)
        navigationItem.titleView = tV
        
    }
    
    // 准备UI
    private func prepareUI() {
        // 添加子控件
        view.addSubview(messageField)
        
        // 布局子控件
        messageField.ff_AlignInner(type: ff_AlignType.TopLeft, referView: view, size: CGSizeMake(view.frame.width - 20 * 2, 200), offset: CGPointMake(20, 64 + 20))
        
        
    }
    
    // 返回
    func back() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 发送消息
    func sendMessage() {
        // POST发送信息
        print("messageField = \(messageField.text!)")
        print("id = \(status?.id)")
        if messageField.text?.characters.count == 0 {
            SVProgressHUD.showErrorWithStatus("内容不能为空", maskType: SVProgressHUDMaskType.Black)
            return
        }
        //        CZNetworkTool.sharedInstance.sendCommentMessage(messageField.text!, id: id!)
        CZNetworkTool.sharedInstance.sendCommentMessage(messageField.text!, id: status!.id) { (result, error) -> () in
            if result != nil {
                // 评论成功
                print("评论成功")
                SVProgressHUD.showSuccessWithStatus("评论成功", maskType: SVProgressHUDMaskType.Clear)
                self.back()
                
            } else {
                // 评论失败
                print("评论失败 \(error)")
                SVProgressHUD.showErrorWithStatus("评论失败", maskType: SVProgressHUDMaskType.Clear)
            }
        }

    }
    
//    // 微博的ID
//    var id:Int?
    
    // 用户昵称
    var status: CZStatus?
    
    // MARK: - 懒加载
    ///  文本框
    lazy var messageField: UITextField = {
        let messageField = UITextField()
        messageField.placeholder = "写评论..."
        messageField.contentVerticalAlignment = UIControlContentVerticalAlignment.Top
//        messageField.n
        return messageField
    }()
    
}
